package com.edu.whty.common.tools;

import com.sensorsdata.analytics.javasdk.SensorsAnalytics;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * SensorsAnalytics 帮手，支持动态初始化
 *
 * @author : lizhengwei
 */
@Component
public class SensorsAnalyticsUtils {
    private static final Logger LOGGER = LogManager.getLogger(SensorsAnalyticsUtils.class);

    private static String PATH;

    @Value("${rrtbk.saServerUrl}")
    public void  setSaServerUrl(String saServerUrl){
        PATH=saServerUrl;
    }


    public static SensorsAnalytics getInstance() {
        if (PATH==null) {
            LOGGER.error("zk节点 api.saServerUrl 不存在");
            return null;
        } else if (StringUtils.isEmpty(PATH)) {
            LOGGER.error("zk节点 api.saServerUrl 值为空");
            return null;
        }
        return SensorsAnalyticsHoler.INSTANCE;
    }


    private static class SensorsAnalyticsHoler {
        static SensorsAnalytics INSTANCE = null;

        static {
            String saServerUrl = PATH;
            LOGGER.warn("zk节点 api.saServerUrl : " + saServerUrl);
            INSTANCE = new SensorsAnalytics(new SensorsAnalytics.BatchConsumer(saServerUrl, 1, true));
        }

    }
}

